import path from 'node:path'
import { defineConfig } from 'vite'
import useUni from '@dcloudio/vite-plugin-uni'
import useUniPages from '@uni-helper/vite-plugin-uni-pages'
import legacy from '@vitejs/plugin-legacy'
import TransformPages from 'uni-read-pages-vite'

import pages from './pages.js'

// https://vitejs.dev/config/
export default defineConfig({
	define: {
			ROUTES: new TransformPages().routes
	},
  plugins: [
		useUni(),
    useUniPages({
      mergePages: false,
      homePage: pages.pages[0].path
    }),
		// legacy 插件仅在 H5 平台使用,不适用于 app 平台
		...(process.env.UNI_PLATFORM === 'h5' ? [
			legacy({
				targets: ['chrome 49']
			})
		] : []),
  ],
  resolve: {
    alias: {
      '^@': path.resolve(__dirname, './src/'),
    },
  },
  css: {
  },
  build: {
    minify: true,
  },
	server: {
		host: '0.0.0.0',
		hmr: true,
		proxy: {
			// 代理用户服务和通用服务的 API 请求
			'/user': {
				target: 'http://113.45.228.8',
				changeOrigin: true,
				secure: false
			},
			'/common': {
				target: 'http://113.45.228.8',
				changeOrigin: true,
				secure: false
			},
			// 保留原有的战斗 API 代理（如果需要的话）
			'/battle-api': {
				target: 'http://113.45.228.8/',
				changeOrigin: true,
				rewrite: path => path.replace(/^\/battle-api/, ''),
			}
		}
	},
})
